check_login()
var params = new URLSearchParams(window.location.search);

var url_cardId = params.get('card_id');

window.onload = function() {

	if (url_cardId != '') {
		document.getElementById("scardid").value = url_cardId;

		serchOrder(0)

	} else {
		getOrderList(0).then(function(data) {
			showOrderList(data, 0);
		}).catch(function(error) {
			console.log(error);
		});
	}


	showHead();
	showLeftMenu();

}

function serchOrder(page) {
	getOrderList(page).then(function(data) {
		showOrderList(data, page);
	}).catch(function(error) {
		console.log(error);
	});
}

function showOrderList(data, page) {
	console.log(data)
	// 获取 <select> 元素的引用
	let selectElement = document.getElementById("order_list");
	selectElement.innerHTML = '';
	let str = '';
	// 使用循环创建选项并添加到 <select> 元素中
	for (let i = 0; i < data.length; i++) {

		if (data[i].status == 0) {
			data[i].status = "未支付";
		} else if (data[i].status == 1) {
			data[i].status = "已支付";
		} else if (data[i].status == 2) {
			data[i].status = "已取消";
		}
		data[i].createTime = timestampToDatetime(data[i].createTime);
		str += `<tr role="row" class="odd">
					<td class="sorting_1" >
						<div class="form-check custom-checkbox ms-2">
							<input type="checkbox" class="form-check-input" id="CheckBox"
								required="" value="${data[i].id}">
							<label class="form-check-label" for="customCheckBox10"></label>
						</div>
					</td>
					<td>${data[i].realName}</td>
					<td class="">${data[i].cardId}</td>
					<td>${data[i].createTime}</td>
					<td>${data[i].orderName}</td>
				
					<td>
						<span class="badge light badge-danger">
							<i class="fa fa-circle text-danger me-1"></i>
							${data[i].status}
						</span>
					</td>
					<td>
						<div class="dropdown ms-auto ">
							<div class="btn-link" data-bs-toggle="dropdown" aria-expanded="false">
								<svg width="24px" height="24px" viewBox="0 0 24 24" version="1.1">
									<g stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
										<rect x="0" y="0" width="24" height="24"></rect>
										<circle fill="#000000" cx="5" cy="12" r="2"></circle>
										<circle fill="#000000" cx="12" cy="12" r="2"></circle>
										<circle fill="#000000" cx="19" cy="12" r="2"></circle>
									</g>
								</svg>
							</div>
							<div class="dropdown-menu dropdown-menu-end" style="margin: 0px;">
								<a class="dropdown-item" href="#" onclick="showFrom(${data[i].id})">审核报告</a>
							</div>
						</div>
					</td>
				</tr>`;
	}

	console.log(page)
	selectElement.innerHTML = str;
	let up = page - 1;
	let down = page + 1;
	let pageStr = '';
	if (up >= 0) {

		console.log("up" + up)
		pageStr += `<a class="paginate_button" onclick="serchOrder(${up})">上一页</a>`;
	}

	if (down >= 1) {
		pageStr += `<a class="paginate_button next" onclick="serchOrder(${down})">下一页</a>`;
	}
	let pages = document.getElementById("pages");
	pages.innerHTML = '';
	console.log(pages)
	pages.innerHTML = pageStr
	console.log(selectElement)
}

function showFrom(id) {

	getOrderInfo(id).then(function(data) {
		console.log(data)
		document.getElementById("id").value = data.id;
		document.getElementById("realname").value = data.realName;
		document.getElementById("cardid").value = data.cardId;
		document.getElementById("order_sn").value = data.orderSn;
		let select = document.getElementById("status");
		console.log(data.status)
		select.innerHTML = '';
		let selectStr = ``;
		if(data.status == 0){
			selectStr = `<option value="0"  selected  =  "selected">待总结</option>
						<option value="1">总结中</option>
						<option value="2">已总结</option>`;
			
		}else if(data.status == 1){
		 selectStr =	`<option value="0">待总结</option>
						<option value="1"  selected  =  "selected">总结中</option>
						<option value="2">已总结</option>`;
		}else if(data.status == 2){
		 selectStr =	`<option value="0">待总结</option>
						<option value="1" >总结中</option>
						<option value="2"  selected  =  "selected">已总结</option>`;
		}
		select.innerHTML = selectStr;
		console.log(document.getElementById("status"))
		let tbody = document.getElementById("row");
		let tbodyStr = '';
		for (var i = 0; i < data.projects.length; i++) {
		
			tbodyStr += `<div class="table-responsive row" style="text-align: center;">
							<div class="col-lg-3">
								<div class="mb-3">
									<label
										class="text-black font-w600 form-label">检查项目
										<span class="required">*</span></label>
									<input type="text" class="form-control"
										readonly id="order_sn" name="order_sn"
										placeholder="检查项目" value="${data.projects[i].projectName}" style="border: 0;">
								</div>
							</div>`;
							
			tbodyStr += `<div class="col-lg-3">
								<div class="mb-3">
									<label
										class="text-black font-w600 form-label">检查科室
										<span class="required">*</span></label>
									<input type="text" class="form-control"
										readonly id="order_sn" name="order_sn"
										placeholder="检查科室" value="${data.projects[i].subjectName}" style="border: 0;">
								</div>
							</div>`
			if(data.projects[i].doctorName != null){
				tbodyStr += `<div class="col-lg-3">
									<div class="mb-3">
										<label
											class="text-black font-w600 form-label">检查医生
											<span class="required">*</span></label>
										<input type="text" class="form-control"
											readonly id="order_sn" name="order_sn"
											placeholder="检查医生" value="${data.projects[i].doctorName}" style="border: 0;">
									</div>
								</div>`;
			}
			
							
							if(data.projects[i].status == 0){
								tbodyStr +=`<div class="col-lg-3">
								<div class="mb-3">
									<label
										class="text-black font-w600 form-label">检查状态
										<span class="required">*</span></label>
									<input type="text" class="form-control"
										readonly id="order_sn" name="order_sn"
										placeholder="检查状态" value="待检查" style="border: 0;">
								</div>
							</div>`
							}else if(data.projects[i].status == 1){
								tbodyStr +=`<div class="col-lg-3">
								<div class="mb-3">
									<label
										class="text-black font-w600 form-label">检查状态
										<span class="required">*</span></label>
									<input type="text" class="form-control"
										readonly id="order_sn" name="order_sn"
										placeholder="检查状态" value="检查中" style="border: 0;">
								</div>
							</div>`
							}else if(data.projects[i].status == 2){
								tbodyStr +=`<div class="col-lg-3">
								<div class="mb-3">
									<label
										class="text-black font-w600 form-label">检查状态
										<span class="required">*</span></label>
									<input type="text" class="form-control"
										readonly id="order_sn" name="order_sn"
										placeholder="检查状态" value="已检查" style="border: 0;">
								</div>
							</div>`
							}if(data.projects[i].tragets != null){
						tbodyStr +=`	<table class="table table-responsive-md">
								<thead>
									<tr>
										<th>名称</th>
										<th>参考值</th>
										<th>参数</th>
									</tr>
								</thead>
								<tbody>`;
								
								
									
									for (var j = 0; j < data.projects[i].targets.length; j++) {
									
										tbodyStr+=`
											<tr>
												<td>${data.projects[i].targets[j].targetName}</td>
												<td>${data.projects[i].targets[j].targetMin} - ${data.projects[i].targets[j].targetMax}
												</td>
												<td>${data.projects[i].targets[j].targetNum}</td>
											</tr>`;
									}
									
									
									}
									tbodyStr+= `</tbody>
							</table>`;
								
								tbodyStr+=`
								
							<div class="col-lg-12">`;
							
							if(data.projects[i].subjectContent != null){
								tbodyStr+=`
								<div class="mb-3">
									<label
										class="text-black font-w600 form-label">科室小结
										<span class="required">*</span></label>
									<textarea rows="3" class="form-control" style="border:0px">${data.projects[i].subjectContent}</textarea>
								</div>`;
							}
						
						tbodyStr+=	`</div>
					</div>
					<hr class="hr">`;
				


		}
		tbodyStr+=`
		<div class="mb-3">
			<label
				class="text-black font-w600 form-label">总检结论
				<span class="required">*</span></label>
			<textarea rows="3" class="form-control" name="content" id="content">${data.reportContent}</textarea>
		</div>`;
		tbody.innerHTML = tbodyStr;
		
		document.getElementById("fromModal").setAttribute("style", "display:block")

		document.getElementById("fromModal").setAttribute("class", "modal fade show")

	}).catch(function(error) {
		console.log(error);
	});
}

function closeFrom() {

	document.getElementById("fromModal").setAttribute("style", "display:none")

	document.getElementById("fromModal").setAttribute("class", "modal fade")
}